using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Deepcode.Flux.Core.Systems.Workflow;
using Deepcode.Flux.Core.Systems.Workflow.Bases;
using Deepcode.Flux.Core.Systems.Workflow.Info;
using Deepcode.Flux.Core.Services.State;
using Deepcode.Flux.Core.Extensions;
using Deepcode.Flux.Core.Systems.Workflow.Interfaces;
using Deepcode.Flux.Core.Systems.Audit.Quick;

public partial class Private_Config_Workflow_ui_actionprop : Deepcode.Flux.Core.UI.AdministrationModulePage
{
	#region Permission Code Override
	public override string RequiredPermissionCode { get { return "FluxWorkflowEdit"; } }
	#endregion
	#region ViewState storage
	/// <summary>
	/// Stores the ID of the handler which this form
	/// is adding a new definition to.
	/// </summary>
	public string HandlerID
	{
		get
		{
			object o = ViewState["HandlerID"];
			if (o == null) return String.Empty;
			return (string)o;
		}
		set
		{
			ViewState["HandlerID"] = value;
		}
	}
	/// <summary>
	/// Stores the ID of the definition being edited
	/// </summary>
	public int DefinitionID
	{
		get
		{
			object o = ViewState["DefinitionID"];
			if (o == null) return -1;
			return (int)o;
		}
		set
		{
			ViewState["DefinitionID"] = value;
		}
	}
	/// <summary>
	/// Stores the ID of the process being edited
	/// </summary>
	public int ProcessID
	{
		get
		{
			object o = ViewState["ProcessID"];
			if (o == null) return -1;
			return (int)o;
		}
		set
		{
			ViewState["ProcessID"] = value;
		}
	}
	/// <summary>
	/// Stores the ID of the tasking record
	/// </summary>
	public int TaskingID
	{
		get
		{
			object o = ViewState["TaskingID"];
			if( o == null ) return -1;
			return (int) o;
		}
		set
		{
			ViewState["TaskingID"] = value;
		}
	}
	/// <summary>
	/// Stores the ID of the exit path 
	/// </summary>
	public int TaskExitPathID
	{
		get
		{
			object o = ViewState["TaskExitPathID"];
			if( o == null ) return -1;
			return (int) o;
		}
		set
		{
			ViewState["TaskExitPathID"] = value;
		}
	}
	/// <summary>
	/// Stores the ID of the exit action having it's properties edited
	/// </summary>
	public int ExitActionID
	{
		get
		{
			object o = ViewState["ExitActionID"];
			if( o == null ) return -1;
			return (int) o;
		}
		set
		{
			ViewState["ExitActionID"] = value;
		}
	}
	#endregion

	private IWorkflowHandlerMethodConfig ConfigClass = null;

	#region Page Load Event
	/// <summary>
	/// Page Load event
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!Page.IsPostBack)
		{
			// Store any state information from the calling listing
			this.HandlerID = GetAttribute("HandlerID", "");
			this.DefinitionID = GetAttribute("DefinitionID", -1);
			this.ProcessID = GetAttribute("ProcessID", -1);
			this.TaskingID = GetAttribute("TaskingID", -1);
			this.TaskExitPathID = GetAttribute("TaskExitPathID", -1);
			this.ExitActionID = GetAttribute("ExitActionID", -1);
			// Load info 
			Handler h = new Handlers().GetHandlerByID( this.HandlerID );
			if( h == null ) GoBack();
			Definition d = new Definitions().GetByID( this.DefinitionID );
			if( d == null ) GoBack();
			Process p = new Processes().GetProcessByID( this.ProcessID );
			if( p == null ) GoBack();
			Tasking t = new Taskings().GetTaskingByID( this.TaskingID );
			if( t == null ) GoBack();
			TaskExitPath tep = new TaskExitPaths().GetTaskExitPathByID( this.TaskExitPathID );
			if( tep == null ) GoBack();
			ExitAction ea = new ExitActions().GetExitActionsByID( this.ExitActionID );
			if( ea == null ) GoBack();
			
			// Display infos
			labHandler.Text = h.Name;
			labDefinition.Text = d.DefName;
			labProcess.Text = p.ProcessName;
			labTasking.Text = t.TaskCaption;
			labExitPath.Text = tep.Caption;
			labMethod.Text = ea.Method;
		}
		
		// Load the exit action record
		ExitAction exitAction = new ExitActions().GetExitActionsByID( this.ExitActionID );
		if( exitAction == null ) GoBack();
		
		// Create engine and get the config method
		WorkflowEngine engine = new WorkflowEngine( this.HandlerID );
		ConfigClass = engine.GetConfigObjectForMethod( exitAction.Method );
		if( ConfigClass == null ) GoBack();

		// Clear dynamic form
		formhost.Controls.Clear();
		// Add all the controls as specified
		foreach( Control ctrl in ConfigClass.AddFormControls( this.Page, HandlerID, DefinitionID, ProcessID ))
			formhost.Controls.Add( ctrl );
	
		// If we have something to edit, load it up when not posting back
		if (!Page.IsPostBack)
		{
			ConfigClass.DeSerialise(exitAction.PropertyBag);
			// Bind the data dictionary
			Dictionary.DataSource = engine.GetDataDictionary();
			Dictionary.DataBind();
		}
	}
	#endregion
	#region Navigation
	/// <summary>
	/// Returns to the listing, restoring state
	/// </summary>
	protected void GoBack()
	{
		bool IsSys = false;
		string url = "";

		Tasking t = new Taskings().GetTaskingByID( this.TaskingID );
		if( t != null ) IsSys = t.SystemTask;

		if (IsSys)
		{
			url = String.Format(
				"ui_systemtask.aspx?HandlerID={0}&DefinitionID={1}&ProcessID={2}&TaskingID={3}",
				this.HandlerID,
				this.DefinitionID,
				this.ProcessID,
				this.TaskingID);
		}
		else
		{
			url = String.Format(
				"ui_exitpath.aspx?HandlerID={0}&DefinitionID={1}&ProcessID={2}&TaskingID={3}&TaskExitPathID={4}",
				this.HandlerID,
				this.DefinitionID,
				this.ProcessID,
				this.TaskingID,
				this.TaskExitPathID);
		}
		
		Response.Redirect(url, true);
	}
	#endregion
	#region Save/Cancel handlers
	/// <summary>
	/// User clicked the back to process list button
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnBack_Click(object sender, EventArgs e)
	{
		GoBack();
	}
	/// <summary>
	/// User clicked the save button, update the data
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnSave_Click(object sender, EventArgs e)
	{
		if (!Page.IsValid) return;

		ExitActions eas = new ExitActions();
		ExitAction ea = eas.GetExitActionsByID( this.ExitActionID );
		if( ea == null ) GoBack();

		ea.PropertyBag = ConfigClass.Serialise();
		eas.Save( ea );

		TaskExitPath exitpath = ea.ParentExitPath;
		Tasking tasking = exitpath.ParentTasking;
		Process process = tasking.ParentProcess;
		
		QuickAudit.RaiseInformation(
			new AuditUser(),
			"Flux", "Workflow Manager",
			"modified properties of exit action ({0}) in exit path ({1}) of tasking ({2}) in process ({3})",
			ea.Method,
			exitpath.Caption,
			tasking.TaskCaption,
			process.ProcessName);

		GoBack();
	}
	#endregion
}
